package org.example.cardgame;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.example.cardgame.entity.Message;
import org.example.cardgame.service.CardOperatorService;
import org.example.cardgame.utils.ImageToBase64;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class CardGameApplication {

    public static void main(String[] args) {
        SpringApplication.run(CardGameApplication.class, args);

        try {
            URI uri = new URI("ws://127.0.0.1:8080/websocket/player"); // 替换为你的WebSocket服务器URI
            WebSocketClient webSocketClient = new WebSocketClient(uri) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    System.out.println("新连接已打开");
                    Map<String, Object> data = new HashMap<>();
                    data.put("type", "C_PlayerInfo");
                    Map<String, Object> data1 = new HashMap<>();
                    data1.put("name","renyan");
                    data1.put("photo", ImageToBase64.getBase64For("3.jpg"));
                    data.put("data", data1);
                    send(JSONObject.toJSONString(data)); // 发送消息到服务器
                    System.out.println("发送玩家信息");
                }

                @Override
                public void onMessage(String message) {
                    System.out.println("接收到消息: " + message);
                    CardOperatorService cardOperatorService = new CardOperatorService();
                    String message1 = cardOperatorService.dealMessage(message);
                    send(message1);
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    System.out.println("连接已关闭");
                }

                @Override
                public void onError(Exception ex) {
                    ex.printStackTrace();
                }
            };

            webSocketClient.connect(); // 连接到WebSocket服务器

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
